-- OddFloor
-- Created by chengb Feb/3/2016
-- 奇怪的地板格子

-- 初始化
function initOddFloor(self)
    self.item:removeAllChildren();

    local dbase = DungeonM.queryElement(self.gridData.element, "dbase");
    if type(dbase) == "table" and type(dbase["need_property"]) == "number" then
        -- 配置了需要道具才出现
        if ItemM.getAmount(ME.user, dbase["need_property"]) <= 0 then
            -- 包裹中没有需求的道具
            return;
        end
    end

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    local pos = self.gridData:getPos();
    local function okFunc()
        -- 播放震动
        local uiDungeonMain = UIDungeonMgr.getCurLevel();
        playShakeEffect(uiDungeonMain, 0.03, 8, 8);

        -- 需要替换图片
        if buildingInfo["icon2"] and buildingInfo["icon2"] ~= "" then
            local changeIconPath = getLevelItemIconPath(buildingInfo["icon2"]);
            self.icon:loadTexture(changeIconPath);
        end

        -- 执行奖励
        DungeonActionM.go("do_building_bonus", pos, nil, { ["remainGrid"] = true });
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("ui_open1");

            -- 打开建筑查看界面
            self:openBuildingView(okFunc);
        end
    end

    self.icon:addTouchEventListener(onClicked);
end

